/* 
 * File:   FilePart.cpp
 * Author: frank
 *
 * Created on 2013年10月23日, 下午8:57
 */

#include"FileMng.h"

//将硬盘扇区号转为文件号
char* SecterPath(char *filename,int num)
{	
	strcpy(filename,"C:\\HD\\sect000.txt");
	int n;
	n=num%10;
	filename[12]='0'+n;
	num=num/10;
	n=num%10;
	filename[11]='0'+n;
	num=num/10;
	filename[10]='0'+num;
	
	return filename;
}

//初始化硬盘
void  HDformat()
{
	int i;
	FILE *fp;
	char filename[25];
	char strDir[50];
	//格式化硬盘上的文件
	for(i=0;i<VOL_HD;i++){
		remove(SecterPath(filename,i));
	}
	for(i=0;i<VOL_HD;i++){
		fp=fopen(SecterPath(filename,i),"w");
		fclose(fp);
	}

	long nowTime;
	nowTime=time(&nowTime);

	//将基本内容：基本目录文件、空闲块文件、用户文件等写入硬盘

	//sect000  基本目录文件
	fp=fopen(SecterPath(filename,SECT_BASE),"w");
	BaseSect baseSect;
	baseSect.HeadIndexSect=SECT_HEADINDEX;
	baseSect.userSect=SECT_USERDIR;
	baseSect.vacantSect=SECT_VACDIR;
	fwrite(&baseSect,sizeof(BaseSect),1,fp);
	fclose(fp);

	
	//sect254  文件头的索引块
	fp=fopen(SecterPath(filename,SECT_HEADINDEX),"w"); 
	HeadIndex hIdx[INDEX_MAX];

	strcpy(hIdx[0].name,"/");
	hIdx[0].PMS=NONE;
	hIdx[0].sects[0]=SECT_USERDIR;
	hIdx[0].type=TYPE_DIR;
	hIdx[0].size=1;
	hIdx[0].modTime=nowTime;
	hIdx[0].rdCnt=0;
	hIdx[0].wrtCnt=0;

	strcpy(hIdx[1].name,"/root");
	hIdx[1].PMS=NONE;
	hIdx[1].sects[0]=SECT_ROOTDIR;
	hIdx[1].type=TYPE_DIR;
	hIdx[1].size=1;
	hIdx[1].modTime=nowTime;
	hIdx[1].rdCnt=0;
	hIdx[1].wrtCnt=0;

	strcpy(hIdx[2].name,"/root/psd");
	hIdx[2].PMS=NONE;
	hIdx[2].sects[0]=SECT_PSDFILE;
	hIdx[2].type=TYPE_FILE;
	hIdx[2].size=1;
	hIdx[2].modTime=nowTime;
	hIdx[2].rdCnt=0;
	hIdx[2].wrtCnt=0;

	strcpy(hIdx[3].name,"/vac");
	hIdx[3].PMS=NONE;
	hIdx[3].sects[0]=SECT_VACDIR;
	hIdx[3].type=TYPE_FILE;
	hIdx[3].size=1;
	hIdx[3].modTime=nowTime;
	hIdx[3].rdCnt=0;
	hIdx[3].wrtCnt=0;

	strcpy(hIdx[4].name,"/online");
	hIdx[4].PMS=NONE;
	hIdx[4].sects[0]=SECT_ONLINE;
	hIdx[4].type=TYPE_FILE;
	hIdx[4].size=1;
	hIdx[4].modTime=nowTime;
	hIdx[4].rdCnt=0;
	hIdx[4].wrtCnt=0;

	for(i=5;i<INDEX_MAX;i++){
		hIdx[i].type=TYPE_NONE;
	}


	fwrite(&hIdx,sizeof(HeadIndex),INDEX_MAX,fp);
	fclose(fp);

	//sect255  用户目录
	fp=fopen(SecterPath(filename,SECT_USERDIR),"w"); 
	//初始时，只有root用户
	strcpy(strDir,"-------");
	buildDir(strDir,"root","no",'d',nowTime);
	fwrite(strDir,sizeof(char),50,fp);
	fclose(fp);

	
	//sect256  VacantBLock目录
	fp=fopen(SecterPath(filename,SECT_VACDIR),"w");
	VacantTable VacantBlock;
	//初始时，只有两个空闲块
	VacantBlock.sectNum=2;
	VacantBlock.start[0]=1;  //这里这一位作为起始块号
	VacantBlock.len[0]=SECT_HEADINDEX-1;     //这里这一位作为连续块数量
	VacantBlock.start[1]=SECT_ONLINE+1;  
	VacantBlock.len[1]=VOL_HD-SECT_ONLINE-1;    
	fwrite(&VacantBlock,sizeof(VacantTable),1,fp);
	fclose(fp);



	//sect257  root目录
	fp=fopen(SecterPath(filename,SECT_ROOTDIR),"w");
	//初始时，root用户下只有密码文件
	strcpy(strDir,"-------");
	buildDir(strDir,"psd","no",'f',nowTime);
	fwrite(strDir,sizeof(char),50,fp);
	fclose(fp);

	//sect258 psd文件
	fp=fopen(SecterPath(filename,SECT_PSDFILE),"w");
	User rootUser[USER_MAX];
	//初始时，密码文件里只有root用户
	strcpy(rootUser[0].name,"root");
	strcpy(rootUser[0].psd,"root");
	for(i=1;i<USER_MAX;i++){
		strcpy(rootUser[i].name,"\0");
		strcpy(rootUser[i].psd,"\0");
	}
	fwrite(&rootUser,sizeof(User),USER_MAX,fp); 
	fclose(fp);

	//sect259 在线用户统计文件
	fp=fopen(SecterPath(filename,SECT_ONLINE),"w");
	char online[USER_MAX*22];
	//初始时，密码文件里只有root用户
	strcpy(online,"\0");
	fwrite(online,sizeof(char),USER_MAX*22,fp); 
	fclose(fp);
}

//初始内存
void  iniMemo()
{
	FILE *fp;
	int i;
	char filename[25];
	
	fp=fopen(SecterPath(filename,SECT_BASE),"r");
	fread(&baseMemo,sizeof(BaseSect),1,fp);
	fclose(fp);
	
	
	for(i=0;i<OPEN_MAX;i++)
		openFileMemo[i].access=FAIL;
	
	openedSize=0;
	openedSum=0;

	
	for(i=0;i<STRMEMO_MAX;i++){
		strcpy(strMemo[i],"\0");
	}

}

int addUser(char*newName,long modTime)
{
	int i,flag=0;
	char psd[20];
	char filename[25],stream[CONTENT_MAX],newDir[50];
	FILE *fp;
	User user[USER_MAX];

	printf("User Name:");
	cin>>newName;
	printf("Password:");
	cin>>psd;
	
	fp=fopen(SecterPath(filename,SECT_PSDFILE),"r");
	fread(&user,sizeof(User),USER_MAX,fp);
	fclose(fp);
	
	for(i=0;i<USER_MAX;i++){
		if(strcmp(user[i].name,newName)==0){
			cout<<"The user name exists ALREADY!\n";
			return FAIL;
		}
	}

	for(i=0;i<USER_MAX;i++){
		if(strcmp(user[i].name,"\0")==0){
			strcpy(user[i].name,newName);
			strcpy(user[i].psd,psd);
                        flag=1;
			break;
		}
	}
	if(flag==0){
            cout<<"TOO MANY users in the system, please delete some users before.";
            return FAIL;
        }
        
	fp=fopen(SecterPath(filename,SECT_PSDFILE),"w");
	fwrite(&user,sizeof(User),USER_MAX,fp);
	fclose(fp);
	
	//
	fp=fopen(SecterPath(filename,SECT_USERDIR),"r");
	fread(stream,sizeof(char),CONTENT_MAX,fp);
	fclose(fp);

	newDir[0]='\0';
	buildDir(newDir,newName,"no",'d',modTime);
	strcat(stream,newDir);

	fp=fopen(SecterPath(filename,SECT_USERDIR),"w");
	fwrite(stream,sizeof(char),CONTENT_MAX,fp);
	fclose(fp);

	return SUCCESS;
}


int delUser(char *name)
{
	int i,n,flag;
	char *p,*q,affirm;
	char filename[25],stream[CONTENT_MAX],line[50];
	FILE *fp;
	User user[USER_MAX];

	printf("Please input the user name to be deleted:");
	cin>>name;
        if(strcmp(name,"root")==0){
            cout<<"NO PERMISSION to delete 'root'\n";
            return FAIL;
        }

	cout<<"\n**********************************\n";
	cout<<"     CONFIRM:[Y]Delete [N]Cancel";
	cout<<"\n**********************************\n";
	while(1){
		cin>>affirm;
		if(affirm=='y' || affirm=='Y')
			break;
		else if(affirm=='n'|| affirm=='N')
			return FAIL;
		else
			printf("The value you entered is NOT valid, please input again:");
	}

	
	fp=fopen(SecterPath(filename,SECT_PSDFILE),"r");
	fread(&user,sizeof(User),USER_MAX,fp);
	fclose(fp);
	
	flag=0;
	for(i=0;i<USER_MAX;i++){
		if(strcmp(user[i].name,name)==0){
			flag=1;
			strcpy(user[i].name,"\0");
			strcpy(user[i].psd,"\0");
			break;
		}
	}
	if(flag==0){
		printf("User name %s does NOT exist!\n",name);
		return FAIL;
	}
	
	fp=fopen(SecterPath(filename,SECT_PSDFILE),"w");
	fwrite(&user,sizeof(User),USER_MAX,fp);
	fclose(fp);
	
	//
	fp=fopen(SecterPath(filename,SECT_USERDIR),"r");
	fread(stream,sizeof(char),CONTENT_MAX,fp);
	fclose(fp);

	p=stream+7;
	n=strlen(name);
	
	while(1){
		for(i=0;;i++){
			if(*(p+i)=='-')
				break;
			else
				line[i]=*(p+i);
		}
		q=p+i+7;
		line[i]='\0';
		flag=0;
		for(i=0;i<n;i++){
			if(line[i]!=name[i]){
				flag=1;
				break;
			}
		}
		if(flag==0){
			if(line[i]=='\t'){
				while(*q!='\0'){
					*p=*q;
					p++;
					q++;
				}
				*p='\0';

				fp=fopen(SecterPath(filename,SECT_USERDIR),"w");
				fwrite(stream,sizeof(char),CONTENT_MAX,fp);
				fclose(fp);
				return SUCCESS;
			}
		}
		p=q;
	}
	return FAIL;

}


void loadHeadIndex(HeadIndex *headIndex)
{
	FILE *fp;
	char filename[25];
	fp=fopen(SecterPath(filename,SECT_HEADINDEX),"r");
	fread(headIndex,sizeof(HeadIndex),INDEX_MAX,fp);
	fclose(fp);
}

void unloadHeadIndex(HeadIndex *headIndex)
{
	FILE *fp;
	char filename[25];
	fp=fopen(SecterPath(filename,SECT_HEADINDEX),"w");
	fwrite(headIndex,sizeof(HeadIndex),INDEX_MAX,fp);
	fclose(fp);
}


int loadToMemo(short sect)
{
	int i;
	FILE *fp;
	char filename[25];
	for(i=0;i<STRMEMO_MAX;i++){
		if(strcmp(strMemo[i],"\0")==0){
			fp=fopen(SecterPath(filename,sect),"r");
			fread(&strMemo[i],sizeof(char),CONTENT_MAX,fp);
			fclose(fp);
			break;
		}
	}
	openedSize++;
	return i;
}

void unloadMemo(short memoNum)
{
	strcpy(strMemo[memoNum],"\0");
	openedSize--;
}




void writeBack(char *stream,short sect)
{
	
	char filename[25];
	FILE *fp;
	fp=fopen(SecterPath(filename,sect),"w");
	fwrite(stream,sizeof(char),CONTENT_MAX,fp);
	fclose(fp);
}


void buildDir(char *strDir,char *filename,char* spms,char ctype,long modTime)
{
	char stype[4],smod[20];
	stype[0]='\t';
	stype[1]=ctype;
	stype[2]='\t';
	stype[3]='\0';
	strcat(strDir,filename);
	strcat(strDir,"\t");
	strcat(strDir,spms);
	strcat(strDir,stype);
	strcat(strDir,ctime(&modTime));
	strcat(strDir,"-------");
}



